# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @File  : 单一聚合stdDevPop操作.py
# @Author: dongguangwen
# @Date  : 2025-06-14 20:37
from pymongo import MongoClient

# 连接到MongoDB（默认端口27017）
client = MongoClient('mongodb://root:root123@192.168.1.119:27017/')

# 创建数据库和集合
db = client['learning_mongodb']
collection = db['users']

pipeline = [
    {
        "$group": {
            "_id": None,
            "std_dev_age": {"$stdDevPop": "$age"}
        }
    }
]
result = list(db.users.aggregate(pipeline))
print(result)
print("年龄标准差（总体）:", round(result[0]["std_dev_age"], 2))


"""
[{'_id': None, 'std_dev_age': 11.595257651298654}]
年龄标准差（总体）: 11.6
"""
